#include "head.h"

/* ********************************************************************************
	init(): Initialize framebuffer device and get display informtion 
		from /dev/fb0 to help user finishing operations.
		Map device to memory, when success, 
		save values to user_defined struct and return a pointer. 
******************************************************************************** */
int init(fb_info *my_fb)
{
	int fd;

	struct fb_var_screeninfo fb_var;

	if ((fd = open(FB_DEVICE, O_RDWR)) < 0)
	{
		fprintf(stderr, "Open %s failed:%s\n", FB_DEVICE, strerror(errno));
		return -1;
	}

	
	if (ioctl(fd, FBIOGET_VSCREENINFO, &fb_var) < 0){
		fprintf(stderr, "Map memory failed:%s\n", strerror(errno));
		return -1;
	}
	
	my_fb -> w = fb_var.xres;
	my_fb -> h = fb_var.yres;
	my_fb -> bpp = fb_var.bits_per_pixel;
#if 1

	printf("宽:%d\t高:%d\t象素位:%d\n", my_fb -> w, my_fb -> h, my_fb -> bpp);
#endif	
	int LEN = (my_fb -> w * my_fb -> h * my_fb -> bpp) / 8;
	my_fb -> fbmem = mmap(0, LEN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

	if (my_fb -> fbmem == MAP_FAILED)
	{
		fprintf(stderr, "Map memory failed:%s\n", strerror(errno));
		return -1;
	}
	
	close(fd);
	
	return 0;
}
